package com.ruoyi.ai.controller;

import java.util.List;
import java.util.Arrays;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import lombok.RequiredArgsConstructor;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.core.validate.QueryGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.ai.domain.vo.WxChatVo;
import com.ruoyi.ai.domain.bo.WxChatBo;
import com.ruoyi.ai.service.IWxChatService;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 聊天记录
 *
 * @author ruoyi
 * @date 2024-04-01
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/ai/chat")
public class WxChatController extends BaseController {

    private final IWxChatService iWxChatService;

    /**
     * 查询聊天记录列表
     */
    @SaCheckPermission("ai:chat:list")
    @GetMapping("/list")
    public TableDataInfo<WxChatVo> list(WxChatBo bo, PageQuery pageQuery) {
        Map<String, Object> params = bo.getParams();
        params.put("orderBySort", "desc");
        return iWxChatService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出聊天记录列表
     */
    @SaCheckPermission("ai:chat:export")
    @Log(title = "聊天记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(WxChatBo bo, HttpServletResponse response) {
        List<WxChatVo> list = iWxChatService.queryList(bo);
        ExcelUtil.exportExcel(list, "聊天记录", WxChatVo.class, response);
    }

    /**
     * 获取聊天记录详细信息
     *
     * @param chatId 主键
     */
    @SaCheckPermission("ai:chat:query")
    @GetMapping("/{chatId}")
    public R<WxChatVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long chatId) {
        return R.ok(iWxChatService.queryById(chatId));
    }

    /**
     * 新增聊天记录
     */
    @SaCheckPermission("ai:chat:add")
    @Log(title = "聊天记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody WxChatBo bo) {
        return toAjax(iWxChatService.insertByBo(bo));
    }

    /**
     * 修改聊天记录
     */
    @SaCheckPermission("ai:chat:edit")
    @Log(title = "聊天记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody WxChatBo bo) {
        return toAjax(iWxChatService.updateByBo(bo));
    }

    /**
     * 删除聊天记录
     *
     * @param chatIds 主键串
     */
    @SaCheckPermission("ai:chat:remove")
    @Log(title = "聊天记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{chatIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] chatIds) {
        return toAjax(iWxChatService.deleteWithValidByIds(Arrays.asList(chatIds), true));
    }
}
